package com.wawei.seeds;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

import com.wawei.seeds.utils.PrefUtils;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 *
 * Created by t27891 on 15/7/3.
 */
public class SpashActivity extends ActionBarActivity {
    private static final int SCHEDULER_THREAD = 1;
    private static final int SPLASH_PERIOD = 3;

    private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(SCHEDULER_THREAD);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walnut_splash);
        initTimer();
    }

    private void initTimer(){
        final Runnable worker = new Runnable() {
            @Override
            public void run() {
                //Do nothing ;
            }
        };

        final ScheduledFuture future = scheduler.scheduleAtFixedRate(worker, 0, SPLASH_PERIOD, TimeUnit.SECONDS);
        scheduler.schedule(new Runnable() {
            @Override
            public void run() {
                future.cancel(true);
//                if(PrefUtils.getUserId(SpashActivity.this) > 0) {
//                    Intent mainIntent = new Intent(SpashActivity.this, WalnutMainActivity.class);
//                    SpashActivity.this.startActivity(mainIntent);
//                }
//                else{
                    Intent regIntent = new Intent(SpashActivity.this, RegistrationActivity.class);
                    SpashActivity.this.startActivity(regIntent);
//                }
                SpashActivity.this.finish();
            }
        }, SPLASH_PERIOD, TimeUnit.SECONDS);
    }

    @Override
    protected void onDestroy() {
        scheduler = null;
        super.onDestroy();
    }
}
